package cn.ecnu.paper.controller;


import cn.ecnu.common.Permission;
import cn.ecnu.paper.interceptor.Own;
import cn.ecnu.paper.pojo.Field;
import cn.ecnu.paper.service.FieldService;
import cn.ecnu.paper.vo.FieldFrontVo;
import cn.ecnu.paper.vo.FieldUpdateVo;
import cn.ecnu.paper.vo.ViewFieldVo;
import cn.ecnu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/field")
public class FieldController {

    @Autowired
    private FieldService fieldService;

    // 获取所有邻域, 按照树形排列
    // @Own(permission = Permission.VIEW)
    @GetMapping("/list")
    public R getFieldList() {
        ViewFieldVo vo = fieldService.getFieldList();
        return R.ok().put("data", vo);
    }

    // 新增研究方向
    // @Own(permission = Permission.ADMIN)
    @PostMapping("/add")
    public R addField(@RequestBody FieldFrontVo vo) {
        Field field = fieldService.insertField(vo);
        return R.ok().put("data", field);
    }

    // TODO: @Own(permission = Permission.ADMIN)
    @GetMapping("/delete/{fieldId}")
    public R deleteField(@PathVariable("fieldId") Integer fieldId) {
        fieldService.deleteById(fieldId);
        return R.ok();
    }

    @PostMapping("/update")
    public R updateField(@RequestBody Field vo) {
        fieldService.updateField(vo);
        return R.ok();
    }
}
